import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root',
})
export class AuthService {
  constructor(private http: HttpClient) {}

  getHello(): Observable<string> {
    return this.http.get<string>(environment.apiUrl);
  }

  login(param: any): Observable<any> {
    return this.http.post<any>(environment.apiUrl + '/auth/login', param);
  }

  setCredentials(credentials: any): void {
    sessionStorage.setItem(
      'credentials',
      btoa(encodeURIComponent(JSON.stringify(credentials)))
    );
  }
  getCredentials(): any {
    return sessionStorage.getItem('credentials')
      ? JSON.parse(
          decodeURIComponent(atob(sessionStorage.getItem('credentials') || ''))
        )
      : null;
  }

  changePassword(param: any): Observable<Account> {
    return this.http.post<Account>(
      environment.apiUrl + '/auth/changePassword',
      param
    );
  }

  getLogs(params: any): Observable<{ data: any[]; total: number }> {
    return this.http.get<{ data: any[]; total: number }>(
      environment.apiUrl + `/logger`,
      { params }
    );
  }
}
